//
//  ConnectModel.m
//  LaoGanMa_v2
//
//  Created by 刘畅 on 14-12-18.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ConnectModel.h"
#import "URLMacro.h"
@implementation ConnectModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.myData = [NSMutableData data];
    }
    return self;
}

+ (NSString *)urlWithStr:(NSString *)urlStr parmaters:(NSDictionary *)parmaters
{
    if ([[parmaters allKeys] count] != 0) {
        urlStr = [NSString stringWithFormat:@"%@?", urlStr];
        for (NSString * key in parmaters) {
            urlStr = [NSString stringWithFormat:@"%@%@=%@&", urlStr, key, [parmaters objectForKey:key]];
        }
        urlStr = [urlStr substringToIndex:[urlStr length] - 1];
    }
    urlStr = [urlStr stringByReplacingOccurrencesOfString:@"/" withString:@""];
    return urlStr;
}

+ (void)connectModelWithUrl:(NSString *)str parmaters:(NSDictionary *)parmaters finish:(ConnectBlock)block
{
        ConnectModel * p = [[ConnectModel alloc] init];
        p.myBolck = block;
        [p startConnectWithUrlStr:str parmaters:parmaters];
}

- (void)startConnectWithUrlStr:(NSString *)str parmaters:(NSDictionary *)parmaters
{
    if ([[parmaters allKeys] count] != 0) {
        str = [NSString stringWithFormat:@"%@?", str];
        for (NSString * key in parmaters) {
            str = [NSString stringWithFormat:@"%@%@=%@&", str, key, [parmaters objectForKey:key]];
        }
        str = [str substringToIndex:[str length] - 1];
    }

    NSURL * url = [NSURL URLWithString:str];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection connectionWithRequest:request delegate:self];
    
}

+ (void)uploadWithImageName:(NSString *)name imageData:(NSData *)imageData finish:(ConnectBlock)block
{
    ConnectModel * p = [[ConnectModel alloc] init];
    p.myBolck = block;
    [p startFormConnectWithImageName:name imageData:imageData];
}

- (void)startFormConnectWithImageName:(NSString *)name imageData:(NSData *)imageData
{
    NSURL * url = [NSURL URLWithString:URL_UPLOAD_CONTENT_IMG];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *formLine = @"--0xKhTmLbOuNdArY-44DF1AB4-7622-4163-948C-8A1FEADDBF27";
    NSMutableString *value = [NSMutableString stringWithFormat:@"%@\r\n", formLine];
    [value appendFormat:@"Content-Disposition: form-data; name=\"upfile\"; filename=\"%@\"\r\n", name];
        //声明上传文件的格式
    [value appendFormat:@"Content-Type: image/*\r\n\r\n"];
    NSMutableData *data = [NSMutableData dataWithData:[value dataUsingEncoding:NSUTF8StringEncoding]];
    [data appendData:imageData];
    [data appendData:[[NSString stringWithFormat:@"\r\n%@--", formLine] dataUsingEncoding:NSUTF8StringEncoding]];
//    NSLog(@"value ====== %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    [request setHTTPBody:data];
    NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",@"0xKhTmLbOuNdArY-44DF1AB4-7622-4163-948C-8A1FEADDBF27"];
    [request setValue:content forHTTPHeaderField:@"Content-Type"];
    [NSURLConnection connectionWithRequest:request delegate:self];
}



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_myData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//    NSLog(@"%@", [[NSString alloc] initWithData:_myData encoding:NSUTF8StringEncoding]);
    self.myBolck(_myData);
}

+ (NSData *)connectModelWithUrl:(NSString *)urlStr
{
    ConnectModel * p = [[ConnectModel alloc] init];
    return [p SyncRequestWithUrl:urlStr];
}

- (NSData *)SyncRequestWithUrl:(NSString *)urlStr
{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"GET"];
    NSHTTPURLResponse *response;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    return data;
}


@end
